#include "Cats/CatStates/CatState.h"
#include "Cats/CatStates/CatState_Olympic_Skill.h"
#include "Cats/CatSpriteLoader.h"
#include "Cats/BaseCat.h"
#include "Character/Character.h"
#include "Map/GameMap.h"

CatState_Olympic_Skill::CatState_Olympic_Skill(BaseCat* pCatRef):CatState(pCatRef)
{
	_slowStartTimer = -1;
	_isRunningLeft = true; //left direction
	_fastSpeedMultiplier = FAST_SPEED_MULTIPLIER;
	_mapLeftBound = -(2*pCatRef->getTextureRect().size.width);
	_mapRightBound = _pGameMapRef->getScreenWidth()  + (2*pCatRef->getTextureRect().size.width);
	_exitState = false;

}

CatState_Olympic_Skill::~CatState_Olympic_Skill()
{

}

void CatState_Olympic_Skill::onResume()
{
	CatState::onResume();
	//restart slow start for olympic cat
	runOnEnter();
}

void CatState_Olympic_Skill::onPause()
{
	CatState::onPause();

	if (_slowStartTimer <= 0) //if slow start has ended
		//return speed to normal
		_pCatRef->setSpeedMultiplier(_pCatRef->getSpeedMultiplier() / _fastSpeedMultiplier);
}

void CatState_Olympic_Skill::runOnEnter()
{
	//check if this skill have started before
	_slowStartTimer = SLOW_START_DURATION;

	_pCatRef->stopAllActions();
	_pCatRef->runAction(CatSpriteLoader::getCatAnimation(_pCatRef->getCatType(), CAT_ANIM_WALK));
}

void CatState_Olympic_Skill::update(float timeDiff)
{
	if (!_exitState) //guard against on pause/resume
	{
		//still in slow start mode
		if (_slowStartTimer > 0)
		{
			//reduce timer if timer is still counting
			_slowStartTimer -= timeDiff;

			//start the fast speed
			if (_slowStartTimer <= 0)
				_pCatRef->setSpeedMultiplier(_pCatRef->getSpeedMultiplier() * _fastSpeedMultiplier);

			//continue normal movement
			_pCatRef->move(timeDiff);

			//check collision with the character
			if (_pCatRef->getRect().intersectsRect(_pCharRef->getRect()))
			{
				//reduce character health
				_pCharRef->reduceHealth(_pCatRef->getBaseAttackDamage());

				//kill this cat
				_exitState = true;
			}
		}
		else
		{
			//check whether the cat has reached the boundary to change directions
			if (_pCatRef->getPositionX() < _mapLeftBound && _isRunningLeft)
			{
				_isRunningLeft = false;
				//change speed to move to the right
				_pCatRef->setSpeedMultiplier(-_pCatRef->getSpeedMultiplier());
				_pCatRef->flipCatSprite();
			}
			else if (_pCatRef->getPositionX() > _mapRightBound && !_isRunningLeft)
			{
				_isRunningLeft = true;
				//change speed to move to the left
				_pCatRef->setSpeedMultiplier(-_pCatRef->getSpeedMultiplier());
				_pCatRef->flipCatSprite();

				//change a lane
				_pCatRef->setPositionY(_pGameMapRef->getRandomCatStartPoint().y);
			}

			//move at fast speed
			_pCatRef->move(timeDiff);

			//check collision with the character
			if (_pCatRef->getRect().intersectsRect(_pCharRef->getRect()))
			{
				//reduce character health
				_pCharRef->reduceHealth(_pCatRef->getBaseAttackDamage());

				//kill this cat
				_exitState = true;
			}
		}
	}

	if (_exitState)
	{
		_pCatRef->setNextCatState(CAT_DYING);
	}
}

void CatState_Olympic_Skill::onExit()
{
	CatState::onExit();
	_exitState = false;
}
